package io.renren.modules.study.service.impl;

import io.renren.common.utils.R;
import io.renren.modules.schedule.entity.CourseBodyScheduleEntity;
import io.renren.modules.study.common.utils;
import io.renren.modules.study.dao.CourseDao;
import io.renren.modules.study.dao.IntergralDao;
import io.renren.modules.study.dao.MyCourseDao;
import io.renren.modules.study.entity.*;
import io.renren.modules.sys.dao.UserDao;
import io.renren.modules.sys.dto.CollectDto
        ;
import io.renren.modules.study.service.MyCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MycourseServiceImpl implements MyCourseService {

    @Autowired
    MyCourseDao myCourseDao;
    @Autowired
    CourseDao courseDao;
    @Autowired
    UserDao userDao;
    @Autowired
    IntergralDao intergralDao;

    @Override
    public CollectDto getMyCourseByCourseId(CollectDto c) {
        return myCourseDao.getMyCourseByCourseId(c);
    }

    @Override
    public List getCollectionList(Long userId) {
        return myCourseDao.getCollectionList(userId);
    }

    @Override
    public CollectDto getCollectionByCourseId(CollectDto c) {
        return myCourseDao.getCollectionByCourseId(c);
    }

    @Override
    public void saveCollection(CollectDto c) {
        myCourseDao.saveCollection(c);
    }

    @Override
    public CollectDto getMyHistoryByCourseId(CollectDto c) {
        return myCourseDao.getMyHistoryByCourseId(c);
    }

    @Override
    public void saveHistory(CollectDto c) {
        myCourseDao.saveHistory(c);
    }

    @Override
    public void updateHistory(CollectDto c) {
        myCourseDao.updateHistory(c);

    }

    @Override
    public List getMyHistoryList(Long userId) {
        return myCourseDao.getMyHistoryList(userId);
    }

    @Override
    public Course getMyBuyCourseById(Integer courseId, Long userId) {
        return myCourseDao.getMyBuyCourseById(courseId, userId);
    }

    @Override
    public List getCourseList(Long userId) {
        return myCourseDao.getCourseList(userId);
    }

    @Override
    public R buyMyCourse(Long userId, Map map) {
//        //1查询需要消耗的积分。
//        Course course=courseDao.getCoureById(courseId);
//        UserEntity userEntity=userDao.getUserByUserId(userId);


//        //2插入积分消费历史。
//        IntergralHistory ih = new IntergralHistory();
//        ih.setBeforeIntegral(userEntity.getIntegral());
//        ih.setAfterIntegral(lastIntegral);
//        ih.setUserId(Math.toIntExact(userId));
//        ih.setIntergral(-course.getIntegral());
//        ih.setType(0);//0代表增加
//        ih.setUsername(userEntity.getUsername());
//        ih.setRemarks("购买了课程"+course.getTitle()+"，消耗了" + course.getIntegral() + "积分。");
//        intergralDao.savaIntergralHistory(ih);
//        Integer intergralHistoryId=intergralDao.getNewIntergralHistoryId();
//        //3更新我的积分。
//        userEntity.setIntegral(lastIntegral);
//        userDao.updateAuthentication(userEntity);

        //4保存购买的课程
        String courseId = String.valueOf(map.get("courseId"));
        Course my=myCourseDao.getMyBuyCourseById(Integer.valueOf(courseId),userId);
        if(my!=null){
            return R.ok("您已经购买了此课程",102);
        }
        MyCourseEntity myCourseEntity=new MyCourseEntity();
        myCourseEntity.setCourseId(Integer.valueOf(courseId));
        myCourseEntity.setUserId(Math.toIntExact(userId));
        //myCourseEntity.setIntegralHistoryId(intergralHistoryId);
        myCourseDao.saveMyBuyCourse(myCourseEntity);
        return R.ok("购买成功",100);

    }

    @Override
    public CourseScheduleEntity getMCoursechedule(Map map) {
        return myCourseDao.getMCoursechedule(map);
    }

    @Override
    public void saveMyCourseSchedule(Map map) {
        myCourseDao.saveMyCourseSchedule(map);
    }

    @Override
    public void updateThisCourseSchedule(Map map) {
        myCourseDao.updateThisCourseSchedule(map);
    }

    @Override
    public CourseBodyScheduleEntity getMyCourseBodyScheduleEntity(Map map) {
        return myCourseDao.getMyCourseBodyScheduleEntity(map);
    }

    @Override
    public void saveMyCourseBodyScheduleEntity(Map map) {
        myCourseDao.saveMyCourseBodyScheduleEntity(map);
    }

    @Override
    public void updateMyCourseBodySchedule(Map map) {
        CourseBodyScheduleEntity courseBodyScheduleEntity=myCourseDao.getMyCourseBodyScheduleEntity(map);
        String actionType= (String) map.get("actionType");
        if("video".equals(actionType)){
            courseBodyScheduleEntity.setVideoSchedule((Integer) map.get("videoSchedule"));
            courseBodyScheduleEntity.setVideoTime((Integer) map.get("videoTime"));
        }else if("read".equals(actionType)){
            courseBodyScheduleEntity.setIsRead(1);

        }else if("test".equals(actionType)){
            courseBodyScheduleEntity.setIsTest(1);
        }
        courseBodyScheduleEntity.setCourseBodySchedule(utils.getCourseBodySch(courseBodyScheduleEntity));
        //courseBodyScheduleEntity.
        myCourseDao.updateMyCourseBodySchedule(courseBodyScheduleEntity);

        CourseScheduleEntity courseScheduleEntity=myCourseDao.getMCoursechedule(map);
        courseScheduleEntity.setCourseBodyId(courseBodyScheduleEntity.getCourseBodyId());
        courseScheduleEntity.setCourseBodyTime(courseBodyScheduleEntity.getVideoSchedule());
        courseScheduleEntity.setTotalSchedule(setTotalSchedule(courseScheduleEntity));

        myCourseDao.updateMyCourseSchedule(courseScheduleEntity);

    }

    private Float setTotalSchedule(CourseScheduleEntity courseScheduleEntity){
        Map map=new HashMap();
        map.put("courseId",courseScheduleEntity.getCourseId());
        map.put("userId",courseScheduleEntity.getUserId());
        int cb=0;
        float cbsef=0;

        List<CourseBodyScheduleEntity> list=myCourseDao.getMyCourseBodyScheduleEntityBycourseId(map);
        List<CourseBody> courseList=courseDao.getCourseBodyListByCourseId(courseScheduleEntity.getCourseId());

        for(CourseBodyScheduleEntity c:list){
            if(c.getCourseBodySchedule()!=null){
                cbsef=cbsef+c.getCourseBodySchedule();
            }
        }
        for(CourseBody courseBody:courseList){
            if(courseBody.getType()==1){
                cb++;
            }
            if(courseBody.getType()==0&&courseBody.getQuestionTotal()>0){
                cb++;
            }
        }
        float r=cbsef/cb;



        return new Float(r);
    }

    @Override
    public List getCourseNotInMyPlan(Map map) {
        return myCourseDao.getCourseNotInMyPlan(map);
    }

    @Override
    public List getMyCourseScheduleList(Long userId) {
        return myCourseDao.getMyCourseScheduleList(userId);
    }

    private void updateMyCourseSchedule(Map map){
        CourseScheduleEntity courseScheduleEntity=myCourseDao.getMCoursechedule(map);
        courseScheduleEntity.setCourseBodyId((Integer) map.get("courseBodyId"));
        courseScheduleEntity.setCourseBodyTime((Integer) map.get("videoSchedule"));
    }
}

